package com.controller;
import com.dao.UserDao;
import com.model.FUser;
import com.model.Page;
import com.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Resource
    UserDao userDao;
    @RequestMapping(value = "/getUser")
    public String getUser(Integer id, ModelMap model) {
        FUser user = this.userDao.getUserById(id);
        model.addAttribute("user",user);
        return "user";
    }
    @RequestMapping(value = "/register")
    public  String register(){
        return "front/register";
    }

    @RequestMapping(value = "/login")
    public String login(){
        return "front/login";
    }

    //提交登录
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(FUser fUser, ModelMap model, HttpSession httpSession){
        FUser fUser1=userDao.userLogin(fUser);
        if (fUser1 != null) {
//            if(fUser1.getStatus()==1){
//                model.addAttribute("msg","你的账号已被禁用");
//                return "front/login";
//            }else{
//                return "front/index";
//            }
            httpSession.setAttribute("USER_SESSION",fUser1);
            return "redirect:/";//注意这里不是跳转到静态页面，是跳转到首页链接
        } else {
            model.addAttribute("msg", "账号或密码错误，请重试输入！");
            return "front/login";
        }
    }

    //退出登录，清除服务器内存
    @RequestMapping(value = "logout")
    public String logout(HttpSession session){
        //退出登录清除Session
        session.removeAttribute("USER_SESSION");
        //重定向到登录页面的跳转方法
        return "front/login";
    }

    //注册
    @RequestMapping(value = "/doRegister")
    public String doRegister(FUser fUser, ModelMap model){
        try{
            int row=userDao.insertUser(fUser);
            if(row>0){
                model.addAttribute("msg","注册成功，请登录！");
                return "front/login";
            }else {
                model.addAttribute("msg","注册失败，服务器出错请联系管理员");
            }
        }catch (Exception ex){
            model.addAttribute("msg","注册失败，已存在"+fUser.getUserName());
        }
        return "front/register";
    }

    //获取管理员列表
    @GetMapping(value = "/userList")
    public String adminList(ModelMap model, Page page){
//        List<BUser> bUserList=adminDao.userList();
//        model.addAttribute("adminList",bUserList);
        List<FUser> userList=userDao.list(page);
        int total =userDao.total(page);
        page.caculateLast(total);
        model.addAttribute("userList",userList);
        model.addAttribute("page",page);
        model.addAttribute("total",total);
        return "backend/user-list";
    }



    @Resource
    Result result;

    private Result setResult(int status, String msg){
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    //修改管理员信息页面
    @GetMapping(value = "/updateUserView")
    public  String updateUserView(int id, ModelMap model){
        FUser fUser1 =userDao.getUserById(id);
        model.addAttribute("fUser",fUser1);
        return "backend/user-edit";
    }
    //修改管理员信息
    @PostMapping(value = "/updateUser")
    @ResponseBody
    public Result updateAdmin(FUser fUser, ModelMap model){
        try {
            int row =userDao.updateUser(fUser);
            if(row>0){
                return  this.setResult(1,"修改管理员成功！");
            }else {
                return  this.setResult(0,"修改管理员出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }

//删除用户
    @PostMapping(value = "/delUser")
    @ResponseBody
    public Result delAdmin(Integer id, ModelMap model){
        try{
            FUser fUser=userDao.getUserById(id);
            if(fUser==null){
                return this.setResult(0,"用户不存在！");
            }else {
                int row = userDao.deleteUser(id);
                if (row > 0) {
                    return this.setResult(1, "删除用户成功！");
                } else {
                    return this.setResult(0, "删除用户出错，请联系管理员！");
                }
            }
        }catch (Exception ex){
            return this.setResult(0,"删除用户出错，请联系管理员！");
        }
    }

    //添加管理员信息
    @PostMapping(value = "/addUser")
    @ResponseBody
    public Result addAdmin(FUser fUser, ModelMap model){
        try{
            int row =userDao.insertUser(fUser);
            if(row>0){
                return this.setResult(1,"添加管理员成功！");
            }else {
                return this.setResult(2,"添加管理员出错，请联系管理员！");
            }
        }catch (Exception ex){
            return  this.setResult(0,ex.getMessage());
        }
    }
    @GetMapping(value = "/addUserView")
    public String addAdminView(){
        return "backend/user-add";
    }



    //启用禁用用户
    @PostMapping(value = "/updateUserStatus")
    @ResponseBody
    public Result updateUserStatus(FUser fUser, ModelMap model){
        try {
            FUser fUser1 =userDao.getUserById(fUser.getId());
            if (fUser1==null){
                return this.setResult(0,"用户不存在！");
            }else{
                    int row=userDao.updateUserStatus(fUser);
                    if(row>0){
                        return  this.setResult(1,"设置用户成功！");
                    }else {
                        return  this.setResult(0,"设置用户出错");
                    }

            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }


}
